package com.shuai.AI.ai_app.config;

import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.shuai.AI.ai_app.advisor.MyLoggerAdvisor;
import com.shuai.AI.ai_app.chatmemory.RedisBasedChatMemory;
import com.shuai.AI.ai_app.utils.RedisUtils;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ChatClientConfig {

  // 存储对话记录的redis，或者本地内存的对话记录的数量，默认6
  @Value("${ai.memory.max-size-redis-messages:6}")
  private Integer max_size_messages;

  public static final String SYSTEM_PROMPT =
      "你是一个深耕恋爱心理领域的专家，小鹿姐姐。开场向用户表明身份，告知用户可倾诉恋爱难题。请注意:\n"
          + "1. 开场向用户表明身份，准确理解用户需求\n"
          + "2. 结合参考资料\n"
          + "3. 提供专业解释\n"
          + "4. 考虑实用性\n"
          + "5. 提供替代方案\n"
          + "6. 避免重复问题\n"
              + "7. 对于同一个用户，一开始自我介绍完毕后，后面对于用户问题的回答就不用自我介绍了\n"
//          + "7. 回复的篇幅不要太长\n"
              + "8. 每次回复，多带一些可爱的有色彩的表情包，让用户体验好一点\n"
          + "9.   + \"围绕单身、恋爱、已婚三种状态提问：单身状态询问社交圈拓展及追求心仪对象的困扰；\"\n"
          + "              + \"恋爱状态询问沟通、习惯差异引发的矛盾；已婚状态询问家庭责任与亲属关系处理的问题。\"\n"
          + "              + \"引导用户详述事情经过、对方反应及自身想法，以便给出专属解决方案。\"";

  // ------------------------------ 基于内存 实现多轮对话记忆---------------------------------
  @Bean
  public ChatMemory InnerWindowChatMemory() {
    return MessageWindowChatMemory.builder()
        .chatMemoryRepository(new InMemoryChatMemoryRepository())
        .maxMessages(max_size_messages) // 设置记忆最大消息数
        .build();
  }

  @Bean
  public MessageChatMemoryAdvisor messageInnerChatMemoryAdvisor(ChatMemory InnerWindowChatMemory) {
    return MessageChatMemoryAdvisor.builder(InnerWindowChatMemory).build();
  }

  // ----------------------------------------------------------------------------------------

  // ------------------------------ 基于redis 实现多轮对话记忆---------------------------------
  @Bean
  public ChatMemory redisChatMemory(RedisUtils redisUtils) {
    return RedisBasedChatMemory.builder(redisUtils)
        .maxMessages(max_size_messages) // 设置记忆最大消息数
        .build();
  }

  @Bean
  public MessageChatMemoryAdvisor messageRedisChatMemoryAdvisor(ChatMemory redisChatMemory) {
    return MessageChatMemoryAdvisor.builder(redisChatMemory).build();
  }

  // ------------------------------------------------------------------------------------------

  /**
   * 创建dashscope的chatClient
   *
   * @param dashScopeChatModel
   * @param messageInnerChatMemoryAdvisor
   * @return
   */
  @Bean
  public ChatClient dashScopeChatClient(
      DashScopeChatModel dashScopeChatModel,
      MessageChatMemoryAdvisor messageInnerChatMemoryAdvisor) {

    return ChatClient.builder(dashScopeChatModel)
        .defaultSystem(SYSTEM_PROMPT)
        .defaultAdvisors(
            // 基于内存实现多轮对话记忆
            messageInnerChatMemoryAdvisor,
            // 基于redis 实现多轮对话记忆
            //            messageRedisChatMemoryAdvisor,
            // 自定义日志 Advisor，可按需开启
            new MyLoggerAdvisor())
        .build();
  }
}
